package com.javaweb.blogcite.controller;

import com.javaweb.blogcite.aspect.JwtCheck;
import com.javaweb.blogcite.common.ResponseResult;
import com.javaweb.blogcite.pojo.Setting;
import com.javaweb.blogcite.service.SettingService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/api/setting")
public class SettingController {
    @Resource
    private SettingService settingService;

    @GetMapping
    public ResponseResult<Setting> getSetting() {
        Setting settings = settingService.listAllSettings();
        ResponseResult<Setting> result = new ResponseResult<>();
        result.setCode(0);
        result.setMsg("success");
        result.setData(settings);

        return result;
    }

    @JwtCheck
    @PutMapping
    public ResponseResult<Setting> setSetting(@RequestBody Setting setting) {
        boolean success = settingService.updateSetting(Collections.singletonList(setting));
        ResponseResult<Setting> result = new ResponseResult<>();
        if (success) {
            result.setCode(0);
            result.setMsg("success");
        } else {
            result.setCode(500);
            result.setMsg("失败");
        }
        result.setData(setting);
        return result;
    }
}
